package com.animee.jiujiangdict;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.GridView;

import com.animee.jiujiangdict.adapter.PinyinGridAdapter;
import com.animee.jiujiangdict.adapter.PinyinListAdapter;
import com.animee.jiujiangdict.bean.PinYinBean;
import com.animee.jiujiangdict.bean.PinyinWordBean;
import com.animee.jiujiangdict.db.DBManager;
import com.animee.jiujiangdict.utils.AssetsUtils;
import com.animee.jiujiangdict.utils.URLUtils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.util.ArrayList;
import java.util.List;
public class SearchPinyinActivity extends BaseSearchActivity {
    List<String> groupDatas; //表示分组的列表  [A,B,C,D,E,F,G.......]
    List<List<PinYinBean.ResultBean>>childDatas;   //将每组对应的子类列表存放到这个集合
    PinyinListAdapter adapter;
    String url ;   //获取拼音汉字的网址
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        word = "a";
        titleTv.setText(R.string.main_tv_pinyin);
        // 设置ListView监听器
        setExLvListener();
        url = URLUtils.getPinyinurl(word,page,pagesize);
        loadData(url);

    }
    @Override
    public void setGVListener() {
        super.setGVListener();
        //设置上拉加载和下拉刷新
        refreshGv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {

            @Override
            public void onRefresh(PullToRefreshBase<GridView> refreshView) {
                // 上拉加载的监听  在此处完成分页
                if (page<totalpage) {  //当前加载页数小于最大页数，说明还有数据，还能加载
                    page++;
                    url = URLUtils.getPinyinurl(word,page,pagesize);
                    loadData(url);
                }else {
                    refreshGv.onRefreshComplete();
                }
            }
        });


    }
    /*
     * 改变拼音，从网上获取对应的汉字的方法
     * */
    private void getDataAlterWord() {
        List<PinYinBean.ResultBean> groupList = childDatas.get(selGroupPos);
        if (groupList.size()>selChildPos) {
            PinYinBean.ResultBean bean = groupList.get(selChildPos);
            word = bean.getPinyin();
            page = 1;
            //加载网络数据
            url = URLUtils.getPinyinurl(word,page,pagesize);
            loadData(url);
        }
    }
    /*
    * 设置ExpandListView列表监听器的方法
    * */
    private void setExLvListener() {
        exLv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                adapter.setSelectGroupPos(groupPosition);
                adapter.notifyDataSetInvalidated();
                // 获取被点击的位置的内容
                selGroupPos = groupPosition;
                getDataAlterWord();
                return false;
            }
        });
        exLv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                adapter.setSelectGroupPos(groupPosition);
                adapter.setSelectChildPos(childPosition);
                adapter.notifyDataSetInvalidated();
                selGroupPos = groupPosition;
                selChildPos = childPosition;
                getDataAlterWord();
                return false;
            }
        });
    }

    /*
    * 从Assets文件当中读取数据，使用Gson解析，将数据分组进行存储
    * */
    @Override
    public void initData() {
        super.initData();
        groupDatas = new ArrayList<>();
        childDatas = new ArrayList<>();
        String msg = AssetsUtils.getAssetsContent(this, "pinyinlist.txt");
        if (!TextUtils.isEmpty(msg)) {
            PinYinBean pinYinBean = new Gson().fromJson(msg, PinYinBean.class);  //解析数据
            List<PinYinBean.ResultBean> list = pinYinBean.getResult();    // 获取解析数据中想要的集合
            List<PinYinBean.ResultBean>resultList=new ArrayList<>();    //声明每组包含的元素集合
            for (int i = 0; i < list.size(); i++) {
                PinYinBean.ResultBean bean = list.get(i);    // id  key  pinyin
                String pinyin_key = bean.getPinyin_key();
                if (!groupDatas.contains(pinyin_key)) {
                    groupDatas.add(pinyin_key);
                    if (resultList.size()>0) {
                        childDatas.add(resultList);
                    }
                    resultList = new ArrayList<>();
                    resultList.add(bean);
                }else {
                    resultList.add(bean);
                } }
            childDatas.add(resultList);
        }
        //设置适配器
        adapter = new PinyinListAdapter(this, groupDatas, childDatas);
        exLv.setAdapter(adapter);
    }

    /** 获取网络数据失败时，回调用的方法*/
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        List<PinyinWordBean.ResultBean.ListBean> list = DBManager.queryPyWordFromPywordtb(word, page, pagesize);
        refreshDataByGV(list);
    }

}
